﻿#if (WINRT)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;

namespace nRoute.Components
{
    //[ContentProperty(Name = "Items")]
    public class DependencyObjectCollection<T>
        : DependencyObject, IList<T>, ICollection<T>, IEnumerable<T>, INotifyCollectionChanged
        where
            T : DependencyObject
    {
        private readonly ObservableCollection<T> _innerCollection;

        public DependencyObjectCollection()
        {
            _innerCollection = new ObservableCollection<T>();
            _innerCollection.CollectionChanged += InnerCollection_CollectionChanged;
        }

#region Handlers

        private void InnerCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            OnCollectionChanged(e);
        }

        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (this.CollectionChanged != null) this.CollectionChanged(this, e);
        }

#endregion

#region INotifyCollectionChanged Related

        public event NotifyCollectionChangedEventHandler CollectionChanged;

#endregion

#region IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            return _innerCollection.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _innerCollection.GetEnumerator();
        }

#endregion

#region ICollection<T> Related

        public void Add(T item)
        {
            _innerCollection.Add(item);
        }

        public void Clear()
        {
            _innerCollection.Clear();
        }

        public bool Contains(T item)
        {
            return _innerCollection.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _innerCollection.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _innerCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return _innerCollection.Remove(item);
        }

#endregion

#region IList<T> related

        public int IndexOf(T item)
        {
            return _innerCollection.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _innerCollection.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _innerCollection.RemoveAt(index);
        }

        public T this[int index]
        {
            get
            {
                return _innerCollection[index];
            }
            set
            {
                _innerCollection[index] = value;
            }
        }

#endregion

    }
}

#endif